<!DOCTYPE HTML>
<html >
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- jquery -->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css" />
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <!-- layer -->
    <script type="text/javascript" src="/layer/layer.js"></script>
    <!-- md5.js -->
    <script type="text/javascript" src="/js/md5.min.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>
</head>
<body>

<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
        <span id="userTip"> 您还没有登录，请登陆后再操作<br/></span>
    </div>
    <table class="table" id="goodslist">
        <tr>
            <td>商品名称</td>
            <td colspan="3" id="goodsName"></td>
        </tr>
        <tr>
            <td>商品图片</td>
            <td colspan="3"><img  id="goodsImg" width="200" height="200" /></td>
        </tr>
        <tr>
            <td>秒杀开始时间</td>
            <td id="startTime"></td>
            <td >
                <input type="hidden" id="remainSeconds" />
                <span id="seckillCountdown"></span>
            </td>
            <td>
                <button class="btn btn-primary btn-block" type="button" id="buyButton" onclick="doSeckill()">立即秒杀</button>
                <input type="hidden" name="goodsId"  id="goodsId" />
            </td>
        </tr>
        <tr>
            <td>商品原价</td>
            <td colspan="3" id="goodsPrice"></td>
        </tr>
        <tr>
            <td>秒杀价</td>
            <td colspan="3"  id="seckillPrice"></td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td colspan="3"  id="stockCount"></td>
        </tr>
        <tr>
            <td>result</td>
            <td colspan="3"  id="result"></td>
        </tr>
    </table>
</div>
</body>
<script>
    //入口函数
    $(function () {
        //获取到展示的商品的详细信息
        getGoodsDetail();
    });


    function getGoodsDetail() {
        var goodsId = g_getQueryString("goodsId");
        $.ajax({
            url:"/goods/detail/"+goodsId,
            type:"GET",
            success:function (data) {
                if (data.code==0) {
                    //就进行用ajax来添加对应的展示数据
                    addStytle(data.data);
                }else {
                    layer.msg(data.msg);
                }
            },
            error:function () {
                layer.msg("客户端请求有误");
            }

        })
    }

    //来给静态页面添加样式了
    function addStytle(data) {
        var gdSeckillStatus = data;

        var seckillStatus = gdSeckillStatus.seckillStatus;

        var remainSeconds = gdSeckillStatus.remainSeconds;

        var user = gdSeckillStatus.user;

        var goodsSeckill = gdSeckillStatus.goodsSeckill;

        //进行渲染
        if (user){
            $("#userTip").hide();
        }


        $("#goodsName").text(goodsSeckill.goodsName);
        $("#goodsImg").attr("src",goodsSeckill.goodsImg);
        $("#startTime").text(new Date(goodsSeckill.startDate).format("yyyy-MM-dd hh:mm:ss"));
        $("#remainSeconds").val(remainSeconds);
        $("#goodsId").val(goodsSeckill.id);
        $("#goodsPrice").text(goodsSeckill.goodsPrice);
        $("#seckillPrice").text(goodsSeckill.seckillPrice);
        $("#stockCount").text(goodsSeckill.stockCount);

        adjustSeckillStatus();
    }

    function adjustSeckillStatus() {
        //获得秒杀剩余时间
        var remainSeconds = $("#remainSeconds").val();
        if (remainSeconds>0){
            //秒杀还未开始
            $("#buyButton").attr("disabled",true);
            $("#seckillCountdown").text("秒杀倒计时:"+remainSeconds);
            //轮询,让秒杀时间不断减直到==0
             setTimeout(function () {
                $("#remainSeconds").val(remainSeconds-1);
                adjustSeckillStatus();
             },1000)

        }else if (remainSeconds==0){
            //秒杀进行中
            $("#buyButton").attr("disabled",false);
            $("#seckillCountdown").text("秒杀进行中");
        }else{
            //秒杀已经结束
            $("#buyButton").attr("disabled",true);
            $("#seckillCountdown").text("秒杀已结束");
        }
    }

    //doSeckill()秒杀按钮函数
    function doSeckill() {
        g_showLoading();

        //获得秒杀的编号
        var goodsId = $("#goodsId").val();
        //传输到对应的controller
        $.ajax({
            url:"seckill/do_seckill",
            type:"post",
            data:{goodsId:goodsId},
            success:function (data) {
                if (data.data==0){
                    //查询结果是否秒杀成功
                    findSeckillResult(goodsId);
                }else {
                        layer.msg(data.msg);
                }
            },
            error:function () {
                layer.msg("客户端请求有误");
            }
        })
    }

    function findSeckillResult(goodsId) {
        //查询结果
        $.ajax({
            url:"seckill/findResult",
            type:"post",
            data: {goodsId:goodsId},
            success:function (data) {
               var result= data.data;
                if (result<0){
                    //商品已经买完了,秒杀失败
                    layer.msg("对不起，商品已秒杀完");
                }else if (result==0){
                    //程序还在运行中,轮询继续查询秒杀结果
                    setTimeout(function () {
                        findSeckillResult(goodsId);
                    },200);

                }else {
                    //秒杀成功
                    layer.confirm("恭喜你，秒杀成功。查看订单",{btn:["确定","取消"]});
                    function seeOrder() {
                        //result:orderId
                       document.location="/order_detail.htm?orderId="+result;
                    }
                }
            },
            error:function () {
                layer.msg(data.msg);
            }
        })
    }
</script>
</html>
